<script setup>

import {getCurrentInstance, ref, watch} from "vue";
import {deleteDictData, listDictData} from "@/api/system/dict_data";
import DictDataForm from "@/views/system/dict/DictDataForm.vue";

const {proxy} = getCurrentInstance();
const dictDataList = ref([]);
const total = ref(0);
const queryFormRef = ref();
const dictDataFormRef = ref();
const queryForm = ref({
  pageNo: 1,
  pageSize: 10,
  dictCode: null,
  dictDataName: null,
  dictDataValue: null
})
const props = defineProps({
  dictCode: {
    type: String,
    default: '',
    required: true
  }
})

const getList = async () => {
  const {data} = await listDictData(queryForm.value)
  total.value = data.total;
  dictDataList.value = data.list;
}

const openDictData = (dictId, type, dictCode = '') => {
  if (!dictCode && type === 'create') {
    proxy.$modal.warningNotify('请选择字典类型');
    return false;
  }
  dictDataFormRef.value.open(dictId, type, dictCode)
}

const handleQuery = () => {
  queryForm.value.pageNo = 1;
  getList();
}

const handleReset = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
}

const deleteSysDictData = async (dictDataId) => {
  proxy.$modal.confirm('确定删除该字典数据吗？').then(async () => {
    await deleteDictData(dictDataId);
    proxy.$modal.okNotify('删除成功');
    await getList();
  })
}

watch(
    () => props.dictCode,
    (newProps) => {
      if (!newProps) {
        dictDataList.value = [];
        total.value = 0;
        return;
      }
      queryForm.value.dictCode = newProps;
      getList();
    }
)
</script>

<template>
  <div class="app-container">
    <a-form :model="queryForm" ref="queryFormRef" layout="inline" class="!mb-4 !mt-4">
      <a-form-item label="字典标签" name="dictDataName">
        <a-input v-model:value="queryForm.dictDataName" placeholder="请输入字典标签"/>
      </a-form-item>
      <a-form-item label="字典键值" name="dictDataValue">
        <a-input v-model:value="queryForm.dictDataValue" placeholder="请输入字典键值"/>
      </a-form-item>
      <a-form-item>
        <a-space wrap>
          <a-button type="primary" @click="handleQuery" class="flex items-center">
            <icon icon="ep-search" size="14px" color="unset" class="mr-1"/>
            查询
          </a-button>
          <a-button plain @click="handleReset" class="flex items-center">
            <icon icon="ep-refresh" size="14px" color="unset" class="mr-1"/>
            重置
          </a-button>
          <a-button type="primary" plain class="flex items-center"
                    @click="openDictData(null, 'create',dictCode)" v-hasPermission="['system:dict_data:add']">
            <icon icon="ep-plus" size="14px" color="unset" class="mr-1"/>
            新增
          </a-button>
        </a-space>
      </a-form-item>
    </a-form>

    <a-table :data-source="dictDataList" :border="true" size="large">
      <a-table-column data-index="dictDataName" title="字典标签">
        <template #default="{record}">
          <template v-if="record.listClass === 'default'">{{ record.dictDataName }}</template>
          <template v-else>
            <a-tag :color="record.listClass">{{ record.dictDataName }}</a-tag>
          </template>
        </template>
      </a-table-column>
      <a-table-column data-index="dictDataValue" title="字典键值"/>
      <a-table-column data-index="dictType" title="字典编码"/>
      <a-table-column data-index="dictDataSort" title="字典排序"/>
      <a-table-column data-index="remark" title="备注"/>
      <a-table-column data-index="createTime" title="创建时间" width="180px"/>
      <a-table-column fixed="right" title="操作" width="200px">
        <template #default="{record}">
          <div v-if="record.id !== 1">
            <a-button class="ml-2" type="link" link @click="openDictData(record.dictDataId,'update')"
                      v-hasPermission="['system:dict_data:edit']">编辑
            </a-button>
            <a-button class="ml-2" type="link" danger @click="deleteSysDictData(record.dictDataId)"
                      v-hasPermission="['system:dict_data:delete']">
              删除
            </a-button>
          </div>
        </template>
      </a-table-column>
    </a-table>

    <pagination
        v-show="total > 0"
        :total="total"
        v-model:page="queryForm.pageNo"
        v-model:limit="queryForm.pageSize"
        @pagination="getList"/>

        <dict-data-form ref="dictDataFormRef" @success="getList"/>
  </div>
</template>

<style scoped lang="scss">

</style>
